<template>
  <div class="container">
    <el-form :inline="true" size="small">
      <el-form-item>
        <el-input
          type="text"
          placeholder="请输入科室名称"
          v-model="hospitalDeptName"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchHospitalDept"
          >搜索</el-button
        > </el-form-item
      ><br />
      <el-form-item>
        <span>选择医院</span>
      </el-form-item>
      <el-form-item>
        <el-select v-model="hospitalId" placeholder="--全部医院--">
          <el-option
            v-for="hospital in hospitals"
            :key="hospital.id"
            :value="hospital.id"
            :label="hospital.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <span>股东/分销商</span>
      </el-form-item>
      <el-form-item>
        <el-select v-model="shareholder" placeholder="--所有人员--">
          <el-option value="1" label="股东"></el-option>
          <el-option value="2" label="一级分销商"></el-option>
          <el-option value="3" label="二级分销商"></el-option>
        </el-select> </el-form-item
      ><br />
      <el-form-item>
        <div class="block">
          <span class="demonstration">创建日期</span>
          <el-date-picker
            v-model="createTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
          >
          </el-date-picker>
        </div>
      </el-form-item>
    </el-form>
    <div>
      <el-button type="primary" size="small"
        ><router-link to="/hospital/addHospitalDept"
          >新增科室</router-link
        ></el-button
      >
      <el-button type="primary" size="small">导入科室</el-button>
      <el-button type="primary" size="small">导出科室</el-button>
    </div>

    <el-table :data="hospitalDeptList" border style="width: 100%">
      <el-table-column prop="id" label="科室编号" width="180">
      </el-table-column>
      <el-table-column prop="deptname" label="科室名称" width="180">
      </el-table-column>
      <el-table-column prop="deptcreatetime" label="创建时间">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div>
            <el-button type="primary" round icon="el-icon-info" size="mini"
              ><router-link :to="'/hospital/hospitalDeptDetail/' + scope.row.id"
                >详情</router-link
              ></el-button
            >
            <el-button
              type="danger"
              round
              icon="el-icon-delete"
              size="mini"
              @click="deleteHospitalDept(scope.row.id)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next,sizes,jumper,total"
      :page-sizes="[2, 4, 6]"
      :page-size="limit"
      :current-page="page"
      :total="total"
      @current-change="currentChange"
      @size-change="sizeChange"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      url: "http://localhost:8080/jiapei/hos-dept/hospitalDepts",
      hospitalDeptList: [],
      hospitals: [],
      hospitalId: "",
      createTime: [],
      hospitalDeptName: "",
      page: "",
      limit: "",
      total: "",
    };
  },
  mounted() {
    this.getHospitalDeptList();
  },
  methods: {
    getHospitalDeptList() {
      this.$axios({
        url: this.url,
        method: "GET",
        params: {
          page: this.page,
          limit: this.limit,
          deptname: this.hospitalDeptName,
          startTime: this.createTime[0],
          endTime: this.createTime[1],
          hospitalId:this.hospitalId,
        },
      }).then((res) => {
        console.log(res);
        this.hospitalDeptList = res.data.data.records;
        this.total = res.data.data.total;
        this.hospitals = res.data.data.hospitals;
      });
    },
    currentChange(cp) {
      this.page = cp;
      this.getHospitalDeptList();
    },
    sizeChange(size) {
      (this.limit = size), this.getHospitalDeptList();
    },
    searchHospitalDept() {
      this.getHospitalDeptList();
    },
    deleteHospitalDept(id) {
      this.$confirm("确定删除该条信息吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then((res) => {
        this.$axios({
          url: `http://localhost:8080/jiapei/hos-dept/hospitalDept/${id}`,
          method: "DELETE",
        }).then((res) => {
          if (res.data.code == 200) {
            this.$message({ message: res.data.message, type: "success" });
            this.getHospitalDeptList();
          } else if (res.data.code == 210) {
            this.$message({ message: res.data.message, type: "warning" });
            this.getHospitalDeptList();
          }
        });
      });
    },
  },
};
</script>

<style>
.el-table th > .cell {
  text-align: center;
}
.el-table .cell {
  text-align: center;
}
</style>